﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Mail;


namespace VideoWorld
{
    public class UserRegister : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Label ErrorInfo;
        protected System.Web.UI.WebControls.Image Image1;
        protected System.Web.UI.WebControls.TextBox tbName;
        protected System.Web.UI.WebControls.TextBox tbPasswordUp;
        protected System.Web.UI.WebControls.TextBox tbCode;
        protected System.Web.UI.WebControls.Button btRegUser;
        protected System.Web.UI.WebControls.TextBox tbPassword;
        DataAccess.Users users = new DataAccess.Users(Global.ConnectionString);

        private void Page_Load(object sender, System.EventArgs e)
        {
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
            this.btRegUser.Click += new System.EventHandler(this.regTrue_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        /// <summary>
        /// 注册新用户
        /// </summary>
        private void regTrue_Click(object sender, System.EventArgs e)
        {
            Check();
            Register();
        }
        //发送邮件给注册成功的用户
        private void SendMail(String name, String password)
        {
            try
            {
                string mailFrom = "VideoWorld";
                string mailTo = tbName.Text;
                string mailSubject = "来自VideoWorld的密码信";
                string mailBody = String.Format("尊敬的用户您好，您在VideoWorld的用户为：{0}，密码为：{1}", name, password);
                string serverName = "Mail.VideoWorld.com";
                string mailServerUserName = "Admin";
                string mailServerPassword = "123456";

                Common.Mail.SendMail(mailFrom, mailTo, mailSubject, mailBody, serverName, mailServerUserName, mailServerPassword);

                ErrorInfo.Text = String.Format("提示：系统已经将你注册时侯的密码发到了你的邮箱中，请查收!");
            }
            catch
            {
                ErrorInfo.Text = String.Format("提示：系统繁忙请稍后再试!");
            }
        }
        //检查用户输入
        private void Check()
        {
            String code = Session["VerifyCode"] as String;

            if (Business.Users.VerifyText(tbName.Text))
            {
                ErrorInfo.Text = String.Format("提示：帐号不能为空!");
                return;
            }

            if (!Business.Users.IsValidEmail(tbName.Text))
            {
                ErrorInfo.Text = String.Format("提示：帐号应为E-mail格式!");
                return;
            }

            if (Business.Users.VerifyText(tbPassword.Text))
            {
                ErrorInfo.Text = String.Format("提示：密码不能为空!");
                return;
            }

            if (tbPassword.Text != tbPasswordUp.Text)
            {
                ErrorInfo.Text = String.Format("提示：重复密码与前一次不一致!");
                return;
            }

            if (String.Compare(tbCode.Text, code, true) != 0)
            {
                ErrorInfo.Text = String.Format("提示：验证码输入有误!");
                return;
            }
        }

        //用户注册
        private void Register()
        {
            if (users.IsUserExist(tbName.Text))
            {
                btRegUser.Enabled = false;
                bool regResult = users.Register(tbName.Text, tbPassword.Text, 0, DateTime.Now, 0, 0);
                if (regResult)
                {
                    Session["userName"] = tbName.Text;
                    String urlPath = String.Format("RegisterSuccess.aspx");
                    SendMail(tbName.Text, tbPassword.Text);
                    Page.Response.Redirect(urlPath);
                }
                else
                {
                    ErrorInfo.Text = String.Format("提示：由于系统繁忙，注册失败!");
                }
            }
            else
            {
                ErrorInfo.Text = String.Format("提示：此帐号已经存在!");
            }

        }
    }
}

